package com.Managers;


import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;
import com.SoapClasses.SoapUtil;
import com.shareup.R;


public class ConnectionManagerClass {

    public static boolean isRunning = true;

    private static boolean showingNoConnection;

    // VERIFY IF THERE IS CONNECTION TO THE INTERNET
    public static boolean isConnection(Context context) {

        if (context != null) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            return activeNetworkInfo != null
                        && activeNetworkInfo.isConnected();
        }

        return false;
    }

    private final Handler mHandler = new Handler();

    private boolean wasNoConnection;

    public void startChecking(final ImageView ShareBt) {

        isRunning = true;

        //
        new Thread(new Runnable() { // $codepro.audit.disable

            // disallowUnnamedThreadUsage

            @Override
            public void run() {

                // TODO Auto-generated method stub
                while (isRunning) {
                    try {
                        Thread.sleep(10000);
                        mHandler.post(new Runnable() {

                            @Override
                            public void run() {

                                // TODO Auto-generated method stub
                                // Write your code here to update the UI.
                                manageConnection(ShareBt);
                            }
                        });
                    }
                    catch (Exception e) {
                        Log.e("startInternetCheck", e.getMessage()
                                    + "");
                    }
                }
            }
        }).start();
    }

    public void stopChecking() {

        isRunning = false;
    }

    private void manageConnection(ImageView shareImage) {

        Context context = shareImage.getContext();
        boolean isConnected = isConnection(context);

        //
        if (!isConnected) {
            if (!showingNoConnection) {
                wasNoConnection = true;
                showingNoConnection = true;
                String notC = context.getResources().getString(R.string.error_no_connection_message);
                FragmentManagerClass.sMenu.setEnabled(false);
                if (SoapUtil.MENU_VIEW == SoapUtil.MODE_MINE) {
                    if (FragmentManagerClass.aExplore != null) {
                        FragmentManagerClass.aExplore.onNoConnection();
                    }
                    FragmentManagerClass.aMine.onConnection();
                }
                else
                    if (SoapUtil.MENU_VIEW == SoapUtil.MODE_FOLLOWERS) {
                        FragmentManagerClass.aFollow.setNotConnection(notC);
                    }
                    else {
                        if (FragmentManagerClass.aMine != null) {
                            FragmentManagerClass.aMine.onConnection();
                        }
                        FragmentManagerClass.aExplore.onNoConnection();
                    }
                //
                shareImage.setEnabled(false);
            }
        }
        else {
            if (wasNoConnection) {
                showingNoConnection = false;
                if (FragmentManagerClass.aMenu.getProfile().getDrawable() == null)
                    FragmentManagerClass.aMenu.updateUserInformation(false);

                if (SoapUtil.MENU_VIEW == SoapUtil.MODE_MINE) {
                    FragmentManagerClass.aMine.onConnection();

                    if (FragmentManagerClass.aExplore != null) {
                        FragmentManagerClass.aExplore.onConnection();

                    }

                }
                else
                    if (SoapUtil.MENU_VIEW == SoapUtil.MODE_FOLLOWERS) {
                        FragmentManagerClass.aFollow.setBackConnection();

                    }
                    else
                        if (SoapUtil.MENU_VIEW == SoapUtil.MODE_NEWS) {
                            FragmentManagerClass.aExplore.onConnection();

                            if (FragmentManagerClass.aMine != null) {
                                FragmentManagerClass.aMine.onConnection();

                            }
                        }
                        else {

                            FragmentManagerClass.aExplore.onConnection();

                        }

                shareImage.setEnabled(true);

                wasNoConnection = false;
            }

        }
    }

}
